package com.subaiwang.tools;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class MybatisUtils {
    private  static SqlSessionFactory factory;
    static {
        try{
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            factory=new SqlSessionFactoryBuilder().build(is);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /*
    *获取SqlSession对象
     *  */
    public static SqlSession getSqlSession(){
        return factory.openSession(false);
    }
    /*
    *关闭
     */
    public static void closaSession(SqlSession session){
        if(session!=null){
            session.close();
        }

    }
}
